/*
	
	Lucas Martínez Propiedades
	
	..
	2012 © Kazuu
	www.kazuu.com.ar

*/

var counter = 0;
var interval;
var time = 4000;

imprimirFicha = function() {
	$("#ficha").printArea();
}

slider = function(tipo) {
	var j_galeria		= $(".galeria");
	var j_li			= j_galeria.find("ul > li");
	
	j_li.eq(counter).find("a").attr("class","");
	
	if (tipo == "next") { counter == j_li.length - 1 ? counter = 0 : counter++; }
	if (tipo == "previus") { counter == 0 ? counter = j_li.length - 1 : counter--; }
	
	j_li.eq(counter).find("a").attr("class","selected");
	
	var j_propertie		= j_li.eq(counter).find(".propertie-img");
	var j_image_slider	= j_galeria.find(".imagen > .medium");
	var imagen			= j_propertie.attr("src").split("92x71");
		j_image_slider.attr("src",imagen[0] + "380x294" + imagen[1]);
}

nextImage = function() {
	slider("next");
}

previusImage = function() {
	slider("previus");
}

changeImage = function($this) {
	var j_galeria		= $(".galeria");
	var j_li			= j_galeria.find("ul > li");
	
		j_li.eq(counter).find("a").attr("class","");
		$this.find("a").attr("class","selected");
		counter = $this.index();
		
	var j_propertie		= j_li.eq(counter).find(".propertie-img");
	var j_image_slider	= j_galeria.find(".imagen > .medium");
	var imagen			= j_propertie.attr("src").split("92x71");
		j_image_slider.attr("src",imagen[0] + "380x294" + imagen[1]);
}

$(document).ready(function() {
	var geocoder;
	var map;
	function initialize() {
		geocoder = new google.maps.Geocoder();
		var latlng = new google.maps.LatLng(0,0);
		var myOptions = {
			zoom: 15,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			scrollwheel: false,
			scaleControl: false
		}
		codeAddress("Argentina " + direccion);
		map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	}
	
	function codeAddress(address) {
		if (geocoder) {
			geocoder.geocode({'address': address}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					map.setCenter(results[0].geometry.location);
					var marker = new google.maps.Marker({
						map: map,
						position: results[0].geometry.location
					});
				} else alert("Geocode no tuvo éxito por las siguientes razones: " + status);
			});
		}
	}
	
	google.maps.event.addDomListener(window,'load',initialize);
	
	var j_galeria	= $(".galeria");
	var j_next		= j_galeria.find(".next");
	var j_previus	= j_galeria.find(".previus");
	var j_li		= j_galeria.find("ul > li");
		j_li.eq(0).find("a").attr("class","selected");
	
	if (j_li.length > 1) {
		j_next.click(function() { nextImage(); });
		j_previus.click(function() { previusImage(); });
		j_li.click(function() { changeImage($(this)); })
	} else {
		j_next.remove();
		j_previus.remove();
		j_li.eq(0).find("a").css("cursor","default");
	}
});
